filter_poi <- function(type, max_level) {
return(poptimes %>% filter(poptimes[type] != 0 & poptimes[type] <= max_level))
}
normalize <- function(x) {
return( (x - min(x) ) / ( max(x) - min(x) ) * 100)
}
summarize_hours <- function(input, func=mean) {
df = input[,(ncol(input)-(168-1)):ncol(input)]
df = as.data.frame(t(df %>% summarize_each(funs(func))))
df$hour.total = 0:(nrow(df)-1)
days = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
df$day = days[floor((df$hour) / 24) + 1]
df$hour.day = df$hour %% 24
colnames(df)[1] = "popularity"
df$popularity = normalize(df$popularity)
df = df[, c(3, 2, 1, 4)]
return(df)
}
poptimes <- read_csv("tidy-places.csv")
poi <- c("supermarket", "church", "gym", "shopping_mall", "store", "department_store")
filtered <- lapply(poi, filter_poi, 2)
bar <- filter_poi("bar", 1)
restaurant <- filter_poi("restaurant", 1)
supermarket <- as.data.frame(filtered[match("supermarket", poi)])
gym <- as.data.frame(filtered[match("gym", poi)])
shopping_mall <- as.data.frame(filtered[match("shopping_mall", poi)])
department_store <- as.data.frame(filtered[match("department_store", poi)])
store <- union(shopping_mall, department_store)
plot_hours <- function(input, plot_title=NULL, day_filter=NULL) {
x_breaks = seq(0, 24, 6)
y_breaks = seq(0, 100, 10)
if (!is.null(day_filter)) {
input = input %>% filter(day == day_filter)
x_breaks = seq(0, 23, 1)
}
plot = ggplot(data = input, mapping = aes(x=hour.day, y=popularity)) +
geom_bar(stat="identity", fill="dodgerblue4", color="white", width=1) +
facet_wrap(~factor(day,levels=c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")), nrow=1) +
labs(title=plot_title, x="Hour of Day", y="Popularity") +
scale_x_continuous(breaks=x_breaks, limits=c(0, 24), na.value=0) +
scale_y_continuous(breaks=y_breaks, limits=c(0, 100)) +
theme_minimal() +
theme(panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank())
return(plot)
}
plot_hours_daily <- function(input, plot_title=NULL) {
days = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
for(day in days) {
print(plot_hours(input, plot_title, day))
}
}
plot_hours_individual <- function(input) {
for (x in 1:nrow(input)) {
name = input[x,"name"]
addr = input[x,"address"]
print(plot_hours(summarize_hours(input[x,], mean), paste(name, addr, sep=" // ")))
}
}
avg_time_spent <- function(input, type) {
data = input %>% filter(!is.na(time_spent_1) & !is.na(time_spent_2))
t1 = mean(data$time_spent_1)
t2 = mean(data$time_spent_2)
df = data.frame("type"=type, "time_spent_1"=t1, "time_spent_2"=t2)
return(df)
}
Average Time Spent per Location Type
loc <- list(bar, restaurant, supermarket, gym, shopping_mall, department_store)
loc_name <- c("bar", "restaurant", "supermarket", "gym", "shopping mall", "department store")
avg_time <- data.frame("type"=".", "time_spent_1"=0.1, "time_spent_2"=0.1)
for (i in 1:length(loc)) {
avg_time <- union(avg_time, as.data.frame(avg_time_spent(as.data.frame(loc[i]), loc_name[i])))
}
avg_time <- avg_time[2:nrow(avg_time),]
avg_time <- avg_time %>% mutate(time_spent_diff = time_spent_2-time_spent_1)
kable(avg_time %>% arrange(time_spent_1), caption = "Sort by time_spent_1")
Sort by time_spent_1
| supermarket |
15.00000 |
15.00000 |
0.00000 |
| gym |
15.00000 |
58.00000 |
43.00000 |
| shopping mall |
15.83333 |
28.33333 |
12.50000 |
| department store |
18.50000 |
24.50000 |
6.00000 |
| restaurant |
18.76712 |
41.36986 |
22.60274 |
| bar |
42.75000 |
83.00000 |
40.25000 |
kable(avg_time %>% arrange(time_spent_2), caption = "Sort by time_spent_2")
Sort by time_spent_2
| supermarket |
15.00000 |
15.00000 |
0.00000 |
| department store |
18.50000 |
24.50000 |
6.00000 |
| shopping mall |
15.83333 |
28.33333 |
12.50000 |
| restaurant |
18.76712 |
41.36986 |
22.60274 |
| gym |
15.00000 |
58.00000 |
43.00000 |
| bar |
42.75000 |
83.00000 |
40.25000 |
kable(avg_time %>% arrange(time_spent_diff), caption = "Sort by time_spent_diff")
Sort by time_spent_diff
| supermarket |
15.00000 |
15.00000 |
0.00000 |
| department store |
18.50000 |
24.50000 |
6.00000 |
| shopping mall |
15.83333 |
28.33333 |
12.50000 |
| restaurant |
18.76712 |
41.36986 |
22.60274 |
| bar |
42.75000 |
83.00000 |
40.25000 |
| gym |
15.00000 |
58.00000 |
43.00000 |